package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementService;
import com.tanhua.server.service.UserFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementService movementService;
    @Autowired
    private UserFreezeService userFreezeService;
    @Autowired
    private CommentsService commentsService;
    /*
     * 推荐动态
     * */


    /*
     * 发布动态
     * */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        //发布动态
        //判断当前用户是否被冻结发表动态  3就表示冻结的范围
        userFreezeService.checkUserStatus("3", UserHolder.getUserId());
        movementService.publishMovement(movement, imageContent);

        return ResponseEntity.ok(null);
    }

    /*
     * 查询个人动态
     * */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /*查询好友动态
     * */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /*
     * 推荐动态
     * */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }


    /*
     * 根据id查询单条动态
     * */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    /*圈子互动
    *动态点赞,评论,喜欢
    * */
    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount =commentsService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
    /*
    * 喜欢
    * */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer loveCount = commentsService.loveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }


    /*
    * 取消喜欢
    * */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer loveCount =commentsService.disloveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    /*
    * 谁看过我(最近访客)
    * */

    @GetMapping("/visitors")
    public ResponseEntity<List<VisitorsVo>> quaryVisitors() {
          List<VisitorsVo> list= movementService.quaryVisitors();
        return ResponseEntity.ok(list);
    }
}
